package com.example.ordersystem.controller;

import com.example.ordersystem.entity.Food;
import com.example.ordersystem.entity.vo.ResultMessage;
import com.example.ordersystem.service.FoodService;
import com.example.ordersystem.util.ResultUtil;
import com.sun.mail.imap.AppendUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("order/food")
public class FoodController {

    @Autowired
    FoodService foodService;

    @Autowired
    ResultUtil resultUtil;

    /**
     * 首页获取推荐菜品
     */
    @GetMapping("home")
    public ResultMessage homeGet()
    {
       return resultUtil.data(foodService.homeGet());
    }

    /**
     * 添加菜品
     * @param food 菜品对象
     */
    @PutMapping("add")
    public ResultMessage add(@RequestBody Food food)
    {
          foodService.add(food);
          return resultUtil.success("添加成功");
    }
    /**
     * 删除菜品
     */
    @DeleteMapping("del/{id}")
    public ResultMessage del(@PathVariable String id)
    {
        foodService.del(id);
        return resultUtil.success("删除成功");
    }
    /**
     * 修改菜品
     */
    @PostMapping("change")
    public ResultMessage change(@RequestBody Food food)
    {
        foodService.change(food);
        return resultUtil.success("修改成功");
    }
    /**
     * 根据id获取菜品信息
     */
    @GetMapping("/selectById/{id}")
    public ResultMessage selectById(@PathVariable String id)
    {
        return resultUtil.data(foodService.selectById(id));
    }
    /**
     * 分页查询菜品列表
     */
    @GetMapping("/select/{currentPage}")
    public ResultMessage selectByPage(@PathVariable int currentPage)
    {
         return resultUtil.data(foodService.selectByPage(currentPage));
    }
    /**
     * 扣减菜品库存
     */
    @PostMapping("/reduce/{id}/{num}")
    public ResultMessage reduceStock(@PathVariable String id,@PathVariable String num)
    {
        foodService.reduceStock(id,num);
        return resultUtil.success("扣减成功");
    }
    /**
     * 根据名称查询菜品
     */
    @GetMapping("/selectByName/{name}")
    public ResultMessage selectByName(@PathVariable String name)
    {
        List<Food> foodList = foodService.selectByName(name);
        return resultUtil.data(foodList);
    }
    /**
     * 删除多个菜品
     */
    @DeleteMapping("DelMore")
    public ResultMessage mutilDel(@RequestBody List<Food> idList)
    {
        foodService.delMore(idList);
        return resultUtil.success("删除成功");
    }
}
